"""
FastAPI 应用入口
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.endpoints import text, chat, knowledge
from app.models.response import HealthResponse
from app.core.config import settings

# 创建 FastAPI 应用实例
app = FastAPI(
    title="FastAPI 文本问答系统",
    description="基于 OpenAI Embedding 和 Chat API 的文本问答系统",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# 配置 CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 生产环境应该设置具体的域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(
    text.router,
    prefix="/api/v1/text",
    tags=["文本处理"]
)

app.include_router(
    chat.router,
    prefix="/api/v1/chat",
    tags=["问答"]
)

app.include_router(
    knowledge.router,
    prefix="/api/v1/knowledge",
    tags=["知识库管理"]
)


@app.get("/health", response_model=HealthResponse, tags=["系统"])
async def health_check():
    """
    健康检查接口
    
    检查服务运行状态
    """
    return HealthResponse(
        status="healthy",
        version="1.0.0"
    )


@app.get("/", tags=["系统"])
async def root():
    """
    根路径
    
    返回 API 基本信息和文档链接
    """
    return {
        "message": "FastAPI 文本问答系统",
        "version": "1.0.0",
        "docs": "/docs",
        "redoc": "/redoc",
        "health": "/health"
    }


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "app.main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level=settings.LOG_LEVEL.lower()
    )

